--[[
FilePath: \Lua\View\DungeonRandomEventUI\UIRandomEventQuestion.lua
主线随机事件--问答
--]]

require 'View/UIBase/UIBase'

UIRandomEventQuestion = UIBase.New(PanelResNames.UIRandomEventQuestion)
local _M = UIRandomEventQuestion

_M.hasInit = false
_M.saveReward = nil;
_M.selectionDesc = {"f_SelectionOneDesc", "f_SelectionTwoDesc", "f_SelectionThreeDesc"};

function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_BgClose"), self.OnClose);
        self:RegisterOnClick(self:GetChild("Btn_Confirm"), self.OnConfirm);
        self.btnAnswer1 = self:GetChild("Btn_Answer01");
        self:RegisterOnClick(self.btnAnswer1, self.OnAnswer1);
        self.btnAnswer2 = self:GetChild("Btn_Answer02");
        self:RegisterOnClick(self.btnAnswer2, self.OnAnswer2);
        self.btnAnswer3 = self:GetChild("Btn_Answer03");
        self:RegisterOnClick(self.btnAnswer3, self.OnAnswer3);
        self.textQuestion = self:GetChild("Text_Question");

        self.typeCtrl = self:GetController("type");
        self.btnYourChoice = self:GetChild("Btn_YourChoice");
        self.btnCorrectAnswer = self:GetChild("Btn_CorrectAnswer");

        self.eventQuestion = G.dataTable["t_eventquestion"];

        _M.hasInit = true

        self:addEvent();
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

function _M:removeEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

-- 显示
function _M:OnEnable()
    if not self.hasInit then
        self:OnBack();
        return;
    end
    self:refresh();
end

function _M:refresh()
    self.data = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
    if self.data == nil or self.eventQuestion == nil or self.data.id == 0 then
        self:OnBack();
        return;
    end

    self.questionCfg = self.eventQuestion[tostring(self.data.id)];
    if self.questionCfg == nil then
        self:OnBack();
        return;
    end
    self.typeCtrl.selectedIndex = 0;
    self.textQuestion.text = getLanguage(self.questionCfg.f_QuestionDesc);
    if self.questionCfg.f_SelectionOneDesc == nil or self.questionCfg.f_SelectionOneDesc == "" then
        self.btnAnswer1.visible = false;
    else
        self.btnAnswer1.visible = true;
        self.btnAnswer1.text = getLanguage(self.questionCfg.f_SelectionOneDesc);
    end
    if self.questionCfg.f_SelectionTwoDesc == nil or self.questionCfg.f_SelectionTwoDesc == "" then
        self.btnAnswer2.visible = false;
    else
        self.btnAnswer2.visible = true;
        self.btnAnswer2.text = getLanguage(self.questionCfg.f_SelectionTwoDesc);
    end
    if self.questionCfg.f_SelectionThreeDesc == nil or self.questionCfg.f_SelectionThreeDesc == "" then
        self.btnAnswer3.visible = false;
    else
        self.btnAnswer3.visible = true;
        self.btnAnswer3.text = getLanguage(self.questionCfg.f_SelectionThreeDesc);
    end
end

function _M:updateView(msg)
    self.isBuy = false;
    self.isSelect = false;
    if msg ~= nil then
        self.btnYourChoice.text = getLanguage(self.questionCfg[self.selectionDesc[self.selectIndex]]);

        local randomEvent = {}
        randomEvent.questionId = self.data.id;
        randomEvent.selectIndex = self.selectIndex;
        if tonumber(msg.right) == 1 then
            self.typeCtrl.selectedIndex = 1;
            randomEvent.right = 1;
        else
            self.typeCtrl.selectedIndex = 2;
            self.btnCorrectAnswer.text = getLanguage(self.questionCfg[self.selectionDesc[self.questionCfg.f_RightQuestion]]);
            randomEvent.right = 0;
        end
        self.saveReward = msg.items;
        TAManager.TrackByDictionary("dungeonRandomEventQuestion", randomEvent);
    end
end

function _M:OnAnswer1()
    self:OnAnswer(1);
end

function _M:OnAnswer2()
    self:OnAnswer(2);
end

function _M:OnAnswer3()
    self:OnAnswer(3);
end

function _M:OnAnswer(index)
    if self.isSelect then
        return;
    end
    self.isSelect = true;
    self.selectIndex = index;
    if HelperFunc.GetServerTime() > self.data["overdueTime"] then
        UISysTips.AddMsg(getLanguage("ErrorCode_210004"));
        self:OnBack();
    else
        self.isBuy = true;
        -- 发送答案
        DungeonRandomEventHandler.sendDungeonRandomEventQuestion(index);
    end
end

function _M:OnConfirm()
    self:OnClose();
end

function _M:OnClose()
    if self.saveReward ~= nil then
        logicMgr.DungeonRandomEventManager.getFetchReward(self.saveReward, function()
            self:OnBack();
        end);
    else
        self:OnBack();
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false;
    self.isSelect = false;
    self.isBuy = false;
    self.saveReward = nil;
end

return _M;